<template>
  <view class="my">
	<view class="content">
		<view style="padding-top: 100rpx;">
			<image :src="bgSrc" style="width: 500rpx; height: 300rpx; margin-bottom: 50rpx;"></image>
			<button type="primary" style="width: 70%;" @click="goLoginDebounce" >一键登录</button>
		</view>
	</view>
  </view>
</template>

<script>
import http from '@/utils/request.js'
import throttle from '@/uni_modules/uv-ui-tools/libs/function/throttle.js';


export default {
  name: 'my',
  data() {
    return {
		bgSrc:''
	};
  },
  onLoad() {
  	this.getInfo();
  },
  methods:{
	  getInfo(){
		  var that = this;
		  http.post('api/index/login',{}).then(function(data){
			  that.bgSrc = http.common.baseUri+data.data;
		  })
	  },
	  goLoginDebounce(){
		throttle(()=>{
			this.goLogin()
		},2000)
	  },
	  // li wx97517c681c82da3d
	  goLogin(){
			uni.login({
				  provider: 'weixin',
				  success: function (loginRes) {
					if(loginRes.code){
							http.post('api/user/miniLogin',{code:loginRes.code}).then(res=>{
								// 这里登录成功会返回用户信息 以及token
								console.log(res.data.token)
								if(res.code == 1){
									uni.setStorageSync('userinfo',res.data)
									uni.setStorageSync('token',res.data.token);
									uni.showToast({
										title:'登录成功',
										icon:'success'
									})
									console.log('asdsad')
									uni.switchTab({
										url:'/pages/index/index'
									})
								}else{
									uni.showToast({
										title:'登录失败',
										icon:'none'
									})
								}
							})
					}else{
						uni.showToast({
							title:'登录失败',
							icon:'none'
						})
					}
				  }
				});
	  }
  }
}
</script>

<style lang="scss" scoped>
.my {
  width: 100%;
  height: 100vh;
  background-color: #f4f4f2;
  .content {
	  display: flex;
		justify-content: center;
		flex-wrap: wrap;
  }
}
</style>
